package com.core.excel;


import com.core.util.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC;

/**
 * Created by dawn on 2017/11/20.
 */
public class ReadExcelUtils {

    private static final String SUFFIX_2003 = ".xls";

    private static final String SUFFIX_2007 = ".xlsx";

    private Workbook workbook;

    private String fileName;

    private InputStream inputStream;

    private HashMap file;

    public HashMap readExcel(MultipartFile multfile) {

        InputStream input = null;
        Workbook wb = null;
        HashMap contentFile = new HashMap();

        String fileName = multfile.getOriginalFilename();
        if (!StringUtils.hasText(fileName)) {
            throw new IllegalArgumentException("名字解析失败");
        }

        try {
            input = multfile.getInputStream();
            wb = getFileWorkBook(fileName, input);
            contentFile = setSheetlContent(wb);
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("格式错误");
        }

        this.fileName = fileName;
        this.workbook = wb;
        this.inputStream = input;
        this.file = contentFile;

        return contentFile;
    }

    // 判断excel类型 （ls和xlsx，HSSFWorkbook针对xls，XSSFWorkbook针对xlsx）
    private Workbook getFileWorkBook(String name, InputStream inputStream) {
        Workbook wb = null;
        if (name.endsWith(SUFFIX_2003)) {
            wb = getHSSFWorkbook(inputStream);
        } else if (name.endsWith(SUFFIX_2007)) {
            wb = getXSSFWorkbook(inputStream);
        } else {
            throw new IllegalArgumentException("excel解析不支持slsx和xls以外的格式");
        }

        return wb;
    }

    private Workbook getHSSFWorkbook(InputStream inputStream) {
        Workbook wb = null;
        try {
            wb = new HSSFWorkbook(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("xls转HSSFWorkbook错误");
        }

        return wb;
    }

    private Workbook getXSSFWorkbook(InputStream inputStream) {
        Workbook wb = null;
        try {
            wb = new XSSFWorkbook(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("sxls转HSSFWorkbook错误");
        }

        return wb;
    }

    private HashMap setSheetlContent(Workbook wb) {
        HashMap<Integer, HashMap> sheetlContent = new HashMap<>();

        int sheetlSize = wb.getNumberOfSheets();
        for (int i = 0; i < sheetlSize; i++) {
            Sheet st = wb.getSheetAt(i);
            HashMap cellContent = setRowContent(st);
            sheetlContent.put(i, cellContent);
        }

        return sheetlContent;
    }

    private HashMap setRowContent(Sheet sheet) {
        HashMap<Integer, HashMap> rowContent = new HashMap<>();

        int rowSize = sheet.getLastRowNum();
        for (int i = 0; i < rowSize; i++) {
            Row row = sheet.getRow(i);
            HashMap cellContent = setCellContent(row);
            rowContent.put(i, cellContent);
        }

        return rowContent;
    }

    private HashMap setCellContent(Row row) {
        HashMap<Integer, String> cellContent = new HashMap<>();
        int cellSize = row.getLastCellNum();
        for (int j = 0; j < cellSize + 1; j++) {
            Cell cell = row.getCell(j);
            if (null == cell) {
                continue;
            }
            String content = conversionString(cell);
            cellContent.put(j, content);
        }

        return cellContent;
    }

    private String conversionString(Cell cell) {
        String content = "";
        int cellType = cell.getCellType();
        if (cellType == CELL_TYPE_NUMERIC) {
            double numCell = cell.getNumericCellValue();
            content = String.valueOf(numCell);
        } else {
            content = cell.getStringCellValue().trim();
        }

        return content;
    }

    public Workbook getWorkbook() {
        return workbook;
    }

    public String getFileName() {
        return fileName;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public HashMap getFile() {
        return file;
    }


}